#!/bin/bash
# copyright 2025, darkathena@qq.com
# All rights reserved.
# This script is used to extract the centralized *_Om_*.tar.gz *_Server_*.tar.gz *_Symbol*.tar.gz *Kernel*.bin files to the same directory as the original tar package
# 

# GaussDB 通用解压脚本
# 用法: uncompress_gaussdb_generic.sh <tar包路径> <CPU架构>
# 架构支持: x86_64/x86/arm/arm64/aarch/aarch64 (忽略大小写，映射到x86_64和aarch64)
# 功能: 提取集中式的*_Om_*.tar.gz *_Server_*.tar.gz *_Symbol*.tar.gz *Kernel*.bin 这4类文件到原始tar包的相同目录

set -e  # 遇到错误立即退出

# 颜色输出函数
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

log_info() {
    echo -e "${GREEN}[信息]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[警告]${NC} $1"
}

log_error() {
    echo -e "${RED}[错误]${NC} $1"
}

# 检查参数
if [ $# -ne 2 ]; then
    log_error "参数错误!"
    echo "用法: $0 <tar包路径> <CPU架构>"
    echo "架构支持: x86_64/x86/arm/arm64/aarch/aarch64 (忽略大小写)"
    echo "示例: $0 DBS-GaussDB-Kylin-Kernel_xxx.tar.gz x86_64"
    echo "示例: $0 DBS-GaussDB-Kylin-Kernel_xxx.tar.gz arm64"
    exit 1
fi

TAR_FILE="$1"
ARCH_INPUT="$2"

# 检查tar包是否存在
if [ ! -f "$TAR_FILE" ]; then
    log_error "tar包文件不存在: $TAR_FILE"
    exit 1
fi

# 获取tar包的绝对路径和目录
TAR_FULLPATH=$(realpath "$TAR_FILE")
TAR_DIR=$(dirname "$TAR_FULLPATH")
TAR_BASENAME=$(basename "$TAR_FILE" .tar.gz)

log_info "原始tar包: $TAR_FULLPATH"
log_info "工作目录: $TAR_DIR"

# 架构映射 (忽略大小写)
ARCH_LOWER=$(echo "$ARCH_INPUT" | tr '[:upper:]' '[:lower:]')
case "$ARCH_LOWER" in
    x86_64|x86)
        TARGET_ARCH="x86_64"
        ARCH_PATTERN="x86_64|X86"
        ;;
    arm64|arm|aarch64|aarch)
        TARGET_ARCH="aarch64"
        ARCH_PATTERN="aarch64|arm64"
        ;;
    *)
        log_error "不支持的架构: $ARCH_INPUT"
        log_error "支持的架构: x86_64/x86/arm/arm64/aarch/aarch64"
        exit 1
        ;;
esac

log_info "目标架构: $TARGET_ARCH"

# 进入tar包目录
cd "$TAR_DIR"

# 创建临时工作目录
WORK_DIR="gaussdb_extract_$$"
mkdir -p "$WORK_DIR"
cd "$WORK_DIR"

# 第1步: 解压主tar包
log_info "第1步: 解压主tar包..."
tar -xf "$TAR_FULLPATH"

# 查找解压后的目录
MAIN_DIR=$(find . -maxdepth 1 -type d -name "*GaussDB*" | head -n 1)
if [ -z "$MAIN_DIR" ]; then
    log_error "无法找到GaussDB主目录"
    exit 1
fi

log_info "进入目录: $MAIN_DIR"
cd "$MAIN_DIR/packages/"

# 第2步: 解压_all.tar.gz文件
log_info "第2步: 解压_all.tar.gz文件..."
ALL_TAR=$(find . -maxdepth 1 -name "*_all.tar.gz" | head -n 1)
if [ -z "$ALL_TAR" ]; then
    log_error "无法找到*_all.tar.gz文件"
    exit 1
fi

log_info "解压文件: $ALL_TAR"
tar -xf "$ALL_TAR"
rm -f "$ALL_TAR"

# 查找解压后的目录
ALL_DIR=$(find . -maxdepth 1 -type d -name "*_all" | head -n 1)
if [ -z "$ALL_DIR" ]; then
    log_error "无法找到*_all目录"
    exit 1
fi

cd "$ALL_DIR"

# 第3步: 解压_noarch.tar.gz文件
log_info "第3步: 解压_noarch.tar.gz文件..."
NOARCH_TAR=$(find . -maxdepth 1 -name "*_noarch.tar.gz" | head -n 1)
if [ -z "$NOARCH_TAR" ]; then
    log_error "无法找到*_noarch.tar.gz文件"
    exit 1
fi

log_info "解压文件: $NOARCH_TAR"
tar -xf "$NOARCH_TAR"
rm -f "$NOARCH_TAR"

# 查找noarch目录
NOARCH_DIR=$(find . -maxdepth 1 -type d -name "*_noarch" | head -n 1)
if [ -z "$NOARCH_DIR" ]; then
    log_error "无法找到*_noarch目录"
    exit 1
fi

cd "$NOARCH_DIR/repo/"

# 第4步: 解压RPM文件
log_info "第4步: 解压RPM文件..."
RPM_FILE=$(find . -maxdepth 1 -name "*.noarch.rpm" | head -n 1)
if [ -z "$RPM_FILE" ]; then
    log_error "无法找到*.noarch.rpm文件"
    exit 1
fi

log_info "解压RPM文件: $RPM_FILE"
rpm2cpio "$RPM_FILE" | cpio -idmv > /dev/null 2>&1
rm -f "$RPM_FILE"

cd "opt/cloud/GaussDB-kernel/"

# 第5步: 解压架构相关的tar.gz文件
log_info "第5步: 解压架构相关的安装包..."

# 根据架构查找对应的tar.gz文件
if [ "$TARGET_ARCH" = "x86_64" ]; then
    ARCH_TAR=$(find . -maxdepth 1 -name "*ha-x86_64*.tar.gz" | head -n 1)
else
    ARCH_TAR=$(find . -maxdepth 1 -name "*ha-aarch64*.tar.gz" | head -n 1)
fi

if [ -z "$ARCH_TAR" ]; then
    log_error "无法找到架构($TARGET_ARCH)对应的tar.gz文件"
    exit 1
fi

log_info "解压架构文件: $ARCH_TAR"
tar -xf "$ARCH_TAR"
rm -f *install*.tar.gz

# 查找解压后的目录
ARCH_DIR=$(find . -maxdepth 1 -type d -name "*install*" | head -n 1)
if [ -z "$ARCH_DIR" ]; then
    log_error "无法找到安装目录"
    exit 1
fi

cd "$ARCH_DIR"

# 第6步: 解压Server相关的tar.gz文件
log_info "第6步: 解压Server相关文件..."

# 查找所有Server相关的tar.gz文件并解压
find . -maxdepth 1 -name "*Server*.tar.gz" -exec tar -xf {} \;
find . -maxdepth 1 -name "*64bit*.tar.gz" -exec tar -xf {} \;

# 第7步: 查找并复制目标文件
log_info "第7步: 查找并复制目标文件..."

TARGET_FILES_FOUND=0

# 查找*Om*.tar.gz文件
OM_FILES=$(find . -name "*_Om_*.tar.gz" 2>/dev/null)
if [ -n "$OM_FILES" ]; then
    for file in $OM_FILES; do
        log_info "找到_Om_文件: $(basename "$file")"
        cp "$file" "$TAR_DIR/"
        TARGET_FILES_FOUND=$((TARGET_FILES_FOUND + 1))
    done
else
    log_warn "未找到*_Om_*.tar.gz文件"
fi

# 查找*Server*.tar.gz文件
SERVER_FILES=$(find . -name "*_Server_*.tar.gz" 2>/dev/null)
if [ -n "$SERVER_FILES" ]; then
    for file in $SERVER_FILES; do
        log_info "找到_Server_文件: $(basename "$file")"
        cp "$file" "$TAR_DIR/"
        TARGET_FILES_FOUND=$((TARGET_FILES_FOUND + 1))
    done
else
    log_warn "未找到*_Server_*.tar.gz文件"
fi

# 查找*Symbol*.tar.gz文件
SYMBOL_FILES=$(find . -name "*_Symbol_*.tar.gz" 2>/dev/null)
if [ -n "$SYMBOL_FILES" ]; then
    for file in $SYMBOL_FILES; do
        log_info "找到_Symbol_文件: $(basename "$file")"
        cp "$file" "$TAR_DIR/"
        TARGET_FILES_FOUND=$((TARGET_FILES_FOUND + 1))
    done
else
    log_warn "未找到*_Symbol_*.tar.gz文件"
fi

# 查找*Kernel*.bin文件
KERNEL_FILES=$(find . -name "*Kernel*.bin" -o -name "*.bin" 2>/dev/null)
if [ -n "$KERNEL_FILES" ]; then
    for file in $KERNEL_FILES; do
        log_info "找到Kernel文件: $(basename "$file")"
        cp "$file" "$TAR_DIR/"
        TARGET_FILES_FOUND=$((TARGET_FILES_FOUND + 1))
    done
else
    log_warn "未找到*Kernel*.bin文件"
fi

# 第8步: 清理临时目录
log_info "第8步: 清理临时目录..."
cd "$TAR_DIR"
rm -rf "$WORK_DIR"

# 总结
log_info "============== 解压完成 =============="
log_info "原始tar包: $TAR_FILE"
log_info "目标架构: $TARGET_ARCH"
log_info "提取文件数: $TARGET_FILES_FOUND"
log_info "输出目录: $TAR_DIR"

if [ $TARGET_FILES_FOUND -eq 0 ]; then
    log_warn "警告: 未找到任何目标文件!"
    exit 1
fi

log_info "所有文件已提取到: $TAR_DIR"
log_info "请检查以下文件:"
ls -la "$TAR_DIR"/*_Om_*.tar.gz "$TAR_DIR"/*_Server_*.tar.gz "$TAR_DIR"/*_Symbol*.tar.gz "$TAR_DIR"/*.bin 2>/dev/null || true 